//
//  MYVisitorView.swift
//  swift2.0项目开始
//
//  Created by 龚昕炜 on 16/7/28.
//  Copyright © 2016年 龚昕炜. All rights reserved.
//

import UIKit

protocol MYVisitorDelegate: NSObjectProtocol {
    /** 登录代理方法 */
    func didLoginMethod()
    /** 注册代理方法 */
    func didRegisterMethod()
}

class MYVisitorView: UIView {
    
    /** 代理属性 */
    weak var delegate: MYVisitorDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1.0)
        
        /** 添加子控件 */
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /** 添加子控件 */
    func setUpUI() {
        /** 添加顺序不能错，小房子在最上层(后添加)，蒙板遮住转动图片 */
        addSubview(turnImageView)
        addSubview(maskImageView)
        addSubview(homeIconImageView)
        
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        /** 小房子位置 */
        homeIconImageView.center = self.center
        /** 蒙板位置 */
        maskImageView.center = self.center
        /** 转动图片位置 */
        turnImageView.center = self.center
        
        /** 文字label的位置 */
        messageLabel.bounds = CGRectMake(0, 0, 216, 40)
        let messageY = CGRectGetMaxY(turnImageView.frame)
        messageLabel.center = CGPointMake(self.center.x, messageY + 20)
        
        /** 注册按钮的位置 */
        registerButton.frame = CGRectMake(messageLabel.frame.origin.x, CGRectGetMaxY(messageLabel.frame), 100, 30)
        
        /** 登录按钮的位置 */
        let registerX = CGRectGetMaxX(messageLabel.frame)
        loginButton.frame = CGRectMake(registerX - 100, registerButton.frame.origin.y, 100, 30)
    }
    
    /** 给外界提供的修改图片和文字的方法 */
    func setUpInfo(imageName: String, text: String, isHome: Bool = false) {
        
        homeIconImageView.image = UIImage(named: imageName)
        messageLabel.text = text
        
        if (isHome == true) {
            turnImageView.hidden = false
            startAnimation()
        } else {
            turnImageView.hidden = true
        }
    }
    
    /** 开启旋转图片的旋转 */
    func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 10
        
        animation.removedOnCompletion = false
        
        turnImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    /** 运行的时候点击按钮，发送通知的时候， 私有的方法会找不到， 解决，使用@objc */
    /** 注册 */
    func register() {
        delegate?.didRegisterMethod()
    }
    
    /** 登录 */
    func login() {
    delegate?.didLoginMethod()
    }

    /** 蒙板 */
    private lazy var maskImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        img.sizeToFit()
        
        return img
    }()
    
    /** 首页的小房子 */
    private lazy var homeIconImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        
        img.sizeToFit()
        
        return img
    }()
    
    /** 转动图片 */
    private lazy var turnImageView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        img.sizeToFit()
        
        return img
    }()
    
    /** 提示文字 */
    private lazy var messageLabel: UILabel = {
    
       let lbl = UILabel()
        
        lbl.text = "123333312312asdsadasdasdsadasdsadasdas34456645646"
        lbl.font = UIFont.systemFontOfSize(14)
        lbl.textColor = UIColor.darkGrayColor()
        lbl.numberOfLines = 0
        lbl.textAlignment = .Center
        
        return lbl
    }()
    
    /** 注册按钮 */
    private lazy var registerButton: UIButton = {
    
        let button = UIButton()
        
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        button.addTarget(self, action: "register", forControlEvents: .TouchUpInside)
        
        return button
    }()
    
    /** 登录按钮 */
    private lazy var loginButton: UIButton = {
    
        let button = UIButton()
        
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        button.addTarget(self, action: "login", forControlEvents: .TouchUpInside)
        
        return button
    }()
}

